﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PTTK_HTTT_06.BUS;
using System.Data;
using System.Data.SqlClient;

namespace PTTK_HTTT_06.DAO
{
    class CONG_THUC_DB
    {
        #region List<CONG_THUC> LayCongThuc(int _maMonAn)
        public static List<CONG_THUC> LayCongThuc(int _maMonAn)
        {
            DataProvider dataProvider = new DataProvider();
            string sqlStr = "select * from CONG_THUC where MaMonAn = '"+_maMonAn+"'";
            if (!dataProvider.IsConnect())
                dataProvider.Connect();
            DataTable dt = dataProvider.ExcuteQueryDataTable(sqlStr);
            dataProvider.Disconnect();

            if (dt != null && dt.Rows.Count <= 0)
                return null;

            List<CONG_THUC> congThucList = new List<CONG_THUC>();
            for (int i = 0, count = dt.Rows.Count; i < count; i++)
            {
                CONG_THUC congThuc = new CONG_THUC();
                congThuc.MaMonAn = _maMonAn;
                congThuc.MaNguyenLieu = int.Parse(dt.Rows[i]["MaNguyenLieu"].ToString());
                congThuc.SoLuong = float.Parse(dt.Rows[i]["SoLuong"].ToString());

                congThucList.Add(congThuc);
            }

            return congThucList;
        }
        #endregion



        internal static void ThemCongThuc(CONG_THUC _CT)
        {
            DataProvider _provider = new DataProvider();
            _provider.Connect();
            SqlParameter[] _para = 
            {
                new SqlParameter("@MaMonAn",SqlDbType.Int){Value = _CT.MaMonAn},
                new SqlParameter("@MaNguyenLieu",SqlDbType.Int){Value = _CT.MaNguyenLieu},
                new SqlParameter("@SoLuong",SqlDbType.Float){Value = _CT.SoLuong}
            };
            _provider.ExcuteProcedure("usp_ThemCongThuc", _para);
            _provider.Disconnect();
        }
    }
}
